package com.csw.shuanfa.utils.pageResult;

import com.csw.shuanfa.entity.User;

import java.util.ArrayList;
import java.util.List;

public class PageResultListUtils {
    public static void main(String[] args) {
        User user = new User();
        List<User> list = new ArrayList<>();
        PageResult<User> res = getPageResult(user.getPageNo(), user.getPageSize(), list);
    }

    public static <T> PageResult<T> getPageResult(int pageNo, int pageSize, List<T> list) {
        if (pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize < 1) {
            pageSize = 10;
        }
        int startIndex = (pageNo - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, list.size());
        List<T> pageData = list.subList(startIndex, endIndex);

        PageResult result = new PageResult();
        result.setTotal((long) list.size());
        result.setItems(pageData);
        result.setTotalPage((long) Math.ceil((double) list.size() / pageSize));
        return result;
    }
}
